package com.cction.activity.tax

import android.graphics.Color
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.cction.R
import com.cction.comm.LinDividerItemDecoration
import com.cction.comm.TaxCCtionUtils
import com.cction.entry.TaxDetailBean
import com.zhy.adapter.recyclerview.CommonAdapter
import com.zhy.adapter.recyclerview.base.ViewHolder
import kotlinx.android.synthetic.main.tax_detail_main.*
import kotlinx.android.synthetic.main.title_bar.*

class TaxDetailActivity : AppCompatActivity() {
    private val arrayList = ArrayList<TaxDetailBean>()
    private lateinit var adapter: CommonAdapter<TaxDetailBean>


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.tax_detail_main)

        initView()
    }


    private fun initView() {
        val total = intent.getStringExtra("tax_total_edit")
        val minimum = intent.getStringExtra("tax_minimum_edit")
        val insurance = intent.getStringExtra("tax_insurance_edit")
        val specially = intent.getStringExtra("tax_specially_edit")

        title_bar_back.visibility = View.VISIBLE
        title_bar_back.setOnClickListener { finish() }

        title_bar_title.text = "个税详情"
        title_bar_other.visibility = View.GONE


        val curList: java.util.ArrayList<TaxDetailBean> = TaxCCtionUtils.getTaxs(total.toDouble(), minimum.toDouble(), insurance.toDouble(), specially.toDouble())
        //全年收入
        var yTotal = total.toDouble() * 12
        //全年五险一金
        var yInsurance = insurance.toDouble() * 12
        //全年专项扣除
        var ySpecially = specially.toDouble() * 12
        //全年扣税
        var yTax = 0.0
        curList.forEach(fun(bean: TaxDetailBean) {
            yTax += bean.tax
        })
        //全年剩余
        var yRemaining = yTotal - yInsurance - ySpecially - yTax
        tax_money_value.text = "$yTotal"
        tax_tax_money_value.text = "$yTax"
        tax_wx_money_value.text = "$yInsurance"
        tax_zx_money_value.text = "$ySpecially"
        tax_sh_money_value.text = "$yRemaining"

        arrayList.clear()
        arrayList.addAll(curList)

        tex_recycler.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
        tex_recycler.addItemDecoration(LinDividerItemDecoration(this))
        adapter = object : CommonAdapter<TaxDetailBean>(this, R.layout.tax_item_main, arrayList) {
            override fun convert(holder: ViewHolder, bean: TaxDetailBean, position: Int) {
                if (position % 2 == 0) {
                    holder.setBackgroundColor(R.id.tax_item_view, Color.parseColor("#eeeeee"))
                } else {
                    holder.setBackgroundColor(R.id.tax_item_view, Color.parseColor("#FFFFFF"))
                }

                holder.setText(R.id.tax_item_date, "${bean.position}")
                holder.setText(R.id.tax_item_value, "${bean.tax}")
                holder.setText(R.id.tax_item_result, "${bean.result}")
            }
        }
        tex_recycler.adapter = adapter
    }
}